<template>
    <div>
        <h2 style="text-align: center">患者信息</h2>
        <br>
        <center><div style="font-size: larger">
        <span style="margin:0 auto">姓名：{{patient.name}}</span>
            <span v-if="patient.sex==1" style="margin-left: 50px">性别：男</span>
            <span v-else style="margin-left: 50px">性别：女</span>
            <span></span>
        </div></center>
        <br><br><br><br><br><br><br><br><br><br><br><br><br>
        <center>
            <el-form ref="addCaseform" :model="myCase" :rules="rules">
                <el-form-item  style="margin-left: 500px" label="病因:"  prop="diagnose">
                   <el-input v-model="myCase.diagnose" autocomplete="off" style="width: 300px;float: left"></el-input>
                </el-form-item>

            </el-form>
        </center>


        <br><br><br><br><br>
        <center>
            <router-link  :to="{name:'PatientList',}"><el-button type="primary"  style="margin-left: 35px">返回</el-button></router-link>
            <router-link  :to="{name:'DPatientCase',params:{pid:this.patient.id,rid:this.rid}}"><el-button type="primary"  style="margin-left: 35px">查看病历</el-button></router-link>
        <el-button type="primary" style="margin-left: 35px" @click="OnSubmit('addCaseform')"  >诊断</el-button>
        <router-link :to="{name:'Prescribe',params:{pid:this.patient.id,rid:this.rid}}"><el-button type="primary" style="margin-left: 35px" >开药</el-button></router-link>
        </center>
    </div>
</template>

<script>
    import { useStore } from "vuex";
    import { onBeforeRouteUpdate, useRoute, useRouter } from "vue-router";
    import axios from "axios";
    import {ElMessage} from "element-plus";
    export default {
        name: "PatientInf",
        data(){
            return{
                rid:'',
                patient:{
                    id: "",
                    idcard: "",
                    name: "",
                    phone: "",
                    password: "",
                    sex: 0,
                    age: 12,
                    loginName: "",
                    address: "",
                    deleted: 0,
                },
                reason:'',
                way:'',
                myCase:{
                    date: "",
                    diagnose: "",
                    did: "",
                    id: "",
                    pid: ""
                },
                rules:{
                    diagnose:[{ required: true, message: "请填写病因", trigger: "blur" }]
                },
            }
        },
        activated() {
            this.create();

        },
        created(){this.currentTime();},
        methods: {
            create(){
                this.patient.id=localStorage.getItem('pid')
                this.rid=localStorage.getItem('rid')
                axios.get("http://119.29.60.153:8001/doctorservice/patient-info/GetPatientById/"+this.patient.id).then(resp=> {
                    this.patient=resp.data.data.patient;
                })
            },
            OnSubmit(formName){
                this.$refs[formName].validate((valid)=>{
                    if(valid){
                        this.myCase.pid=this.patient.id;
                        this.myCase.did=localStorage.getItem('id');
                        axios.post("http://119.29.60.153:8001/doctorservice/patientcase/writeCase?id="+this.rid,this.myCase).then(resp=> {
                            if(resp.data.success==true){
                                ElMessage.success('添加成功!');
                                this.myCase='';
                            }
                            else {
                                ElMessage.error("添加失败!");

                            }
                        })

                    }else {
                        return false;
                    }
                })
            },
            currentTime() {
                setInterval(this.formatDate, 500);
            },
            formatDate() {
                let date = new Date();
                let year = date.getFullYear(); // 年
                let month = date.getMonth() + 1; // 月
                let day = date.getDate(); // 日
                this.myCase.date=`${year}-${month}-${day}`;
            },

        }
    }
</script>

<style scoped>

</style>